﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using Tunnel_Cross_Section.option_frm;
using Tunnel_Cross_Section.相关类;
using System.IO;

namespace Tunnel_Cross_Section.menue_frm
{
    public partial class new_prj_from : Office2007Form
    {
        DataBase m_db;
        Mfrm mfrm;
        public new_prj_from(DataBase db,Mfrm frm)
        {
            InitializeComponent();
            m_db = db;
            mfrm = frm;
            Load_Controls();
        }
        //拿数据填充控件
        private void Load_Controls()
        {
            string sql = "Select [线路] from [projects] group by [线路]";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                combo_line.Items.Add(dt.Rows[i]["线路"].ToString());
            }
        }

        private void btn_no_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            message_frm frm = new message_frm("确定要将上述数据保存么?", "确定", 1);
            frm.ShowDialog();
            string line = combo_line.Text;
            string section = txt_start.Text + "-" + txt_end.Text;
            int single = 1;
            if (combo_single.Text == "是")
            {
                single = 1;
            }
            else
            {
                single = 0;
            }
            string sql = "select max(id) from projects";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            int id = 0;
            if (dt.Rows[0][0].ToString() == string.Empty)
            {
                id = 1;
            }
            else
            {
                id = Convert.ToInt32(dt.Rows[0][0]) + 1;
            }
            if (frm.yesorno == true)
            {
                try
                {
                    sql = "Insert into [projects] values('" + line + "','" + section + "'," + id + "," + single + ",'','')";
                    m_db.ExecuteSQL(sql);
                }
                catch (Exception )
                {
                    this.Close();
                    return;
                }
                Projects pj = new Projects(line, section);
                mfrm.current_projects.Add(pj);
                mfrm.update_projects();
                mfrm.Label.Text = "新建区间完成";
                pj.id = id;
                try
                {
                    double start, end, step;
                    start = double.Parse(textBoxX1.Text);
                    end = double.Parse(textBoxX2.Text);
                    step = double.Parse(textBoxX3.Text);
                    pj.SetAngleStep(start, end, step);
                    string path = System.Windows.Forms.Application.StartupPath + "\\templatespecial.xls";
                    string newpath = System.Windows.Forms.Application.StartupPath + "\\" + section + "模板.xls";
                    if (File.Exists(newpath) == true)
                    {
                        File.Delete(newpath);
                    }
                    File.Copy(path, newpath);
                }
                catch
                {
                    MessageBox.Show("赋值有错误!");
                }
                
            }
            frm.Close();
            this.Close();
        }







    }
}
